﻿using BLL;
using EncryptHelper;
using MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;

namespace Loginter.ajax
{
    /// <summary>
    /// Login 的摘要说明
    /// </summary>
    public class Login : IHttpHandler,IReadOnlySessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //接收用户名和密码
            string account = context.Request.Form["Account"];
            string password = context.Request.Form["Password"];
            //get
            //string account = context.Request.QueryString["Account"];
            //string password = context.Request.QueryString["Password"];
            //context.Response.Write($"用户名：{account},密码：{password}");

            UserBLL man = new UserBLL();
            CurrenUser user = man.GetUserInfo(account,password);
            if (user!=null)
            {
               context.Session["userInfo"] = user.UserID;
                //context.Response.Write("yes");
                #region JSON简单版
                //context.Response.Write("{\"State\":1,\"Message\":\"登录成功！\"}"); 
                #endregion
                #region JSON简化版的OperateResult对象
                //JavaScriptSerializer js = new JavaScriptSerializer();
                //OperateResult result = new OperateResult();
                //result.State = 1;
                //result.Message = "登录成功！";
                //string json = js.Serialize(result);
                //context.Response.Write(json);
                #endregion
                #region JSON构造函数OperateResult
                JavaScriptSerializer js = new JavaScriptSerializer();
                OperateResult result = new OperateResult(1, "登录成功！");
                string json = js.Serialize(result);
                context.Response.Write(json);
                #endregion
                #region JSON枚举类型OperateResult
                //JavaScriptSerializer js = new JavaScriptSerializer();
                //OperateResult result = new OperateResult(ResultState.Success, "登录成功！");
                //string json = js.Serialize(result);
                //context.Response.Write(json); 
                #endregion
            }
            else
            {
                //context.Response.Write("您输入的账户或者密码有误，请重新输入！");
                #region JSON简单版
                //context.Response.Write("{\"State\":-1,\"Message\":\"您输入的账户或者密码有误，请重新输入！\"}"); 
                #endregion
                #region JSON简化版的OperateResult对象
                //JavaScriptSerializer js = new JavaScriptSerializer();
                //OperateResult result = new OperateResult();
                //result.State = -1;
                //result.Message = "您输入的账户或者密码有误，请重新输入！";
                //string json = js.Serialize(result);
                //context.Response.Write(json);
                #endregion
                #region JSON构造函数OperateResult
                JavaScriptSerializer js = new JavaScriptSerializer();
                OperateResult result = new OperateResult(-1, "您输入的账户或者密码有误，请重新输入！！");
                string json = js.Serialize(result);
                context.Response.Write(json);
                #endregion
                #region JSON枚举类型OperateResult
                //JavaScriptSerializer js = new JavaScriptSerializer();
                //OperateResult result = new OperateResult(ResultState.Error, "您输入的账户或者密码有误，请重新输入！！");
                //string json = js.Serialize(result);
                //context.Response.Write(json); 
                #endregion
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}